/**
source code for the list (ls) command. this command is to display the directory specified by the arguments, defaults to current directory "."
**/

#include <stdio.h>
#include "mpx_supt.h"

int sys_open_dir(char dir_name[]);
int sys_get_entry(char name_buf[], int buf_size, long *file_size_p);
int sys_close_dir(void);
void list_dir(char *dir_path);
void clear_screen();
void paginate();

void list_dir(char *dir_path) //dir_path shoul come from Cmd_Handler.c
{
  //declaring variables
  //int buf_size = 20;
  int error_check;
  char file_name_buf[20];
  long *file_size_p;
  int x=1;
  //open directory specified
  if (!strcmp(dir_path, "")) //if no directory specified, display contents of current
  {
    sys_open_dir('\0');
  }
  else
  {
    sys_open_dir(dir_path); //look up directory to open    
  }
  
  //do block is used to grab each mpx file, display it and look for next
  do{
	if(x%15==0){
		paginate();
	}
    error_check = sys_get_entry(file_name_buf, 20, file_size_p);
    printf("%s \t\t %d", file_name_buf, file_size_p);
	x++;
    } while (error_check != ERR_SUP_NOENTR);
  
  //close directory
  sys_close_dir();

}

void paginate(){
	char wait_buf[2]={'\0'};
	int wbuf_size=2;
	printf("Press enter to continue.\n");
	while(wait_buf[0]!='\n'){
		sys_req(READ, TERMINAL, wait_buf, &wbuf_size);
	}
	clear_screen();
}
	
	


